package com.wchat.controller;

import com.wchat.bean.model.MsgPayload;
import com.wchat.bean.model.enums.ChatTypeEm;
import com.wchat.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Set;

/**
 * @author : willian fu
 * @date : 2021/8/31
 */
@RestController
@RequestMapping("message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 查询历史消息
     * @param size 查询条数
     * @param type 消息类型 SINGLE/GROUP
     * @param msgId 从哪条消息开始查
     * @param id 查询与哪个 用户/群组id的消息
     * @return 符合条件的消息
     */
    @GetMapping("history")
    public Object getHistoryMsg(@RequestParam(defaultValue = "100") Integer size,
                                @RequestParam(defaultValue = "true") Boolean sended,
                                @RequestParam(defaultValue = "SINGLE") ChatTypeEm type,
                                @RequestParam(required = false) String msgId,
                                @RequestParam(required = false) Integer id){

        return messageService.getHistoryMsg(size, sended, type, msgId, id);
    }

    /**
     * 消息接收回执
     * 客户端收到消息后要发送回执确认本条消息成功接收
     * @param msgIds 收到的新消息msgId数组
     * @return
     */
    @PostMapping("confirm")
    public Object confirmMsgRevSuccess(@RequestBody Set<String> msgIds){
        return messageService.confirmMsgRevSuccess(msgIds);
    }
    /**
     * 撤回历史消息
     * @param msgId 消息id
     * @param type 消息类型 GROUP/SINGLE
     * @return 撤回结果
     */
    @DeleteMapping("recall")
    public Object recallHistoryMsg(@RequestParam String msgId, @RequestParam ChatTypeEm type){
        return messageService.recallHistoryMsg(msgId, type);
    }

    /**
     * 发送聊天消息
     * @param msgPayload
     * @return
     */
    @PostMapping
    public Object sendMsg(@RequestBody MsgPayload msgPayload){
        return messageService.sendMsg(msgPayload);
    }
}
